package com.tanhua.server.service;

import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author LiKe
 * @date 2022/1/22 10:52
 */
@Service
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserApi userApi;

    //查询通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //2、获取用户的手机号码
        User user = userApi.findByid(userId);
        vo.setPhone(user.getMobile());

        //3、获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //4、获取用户的APP通知开关数据
        Settings settings = settingsApi.findByUserId(userId);
        if(settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;

    }
    //保存问题
    public void saveQuestion(String content) {
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        //2. 根据用户id查询是否有问题
        Question question = questionApi.findByUserId(userId);
        if (question != null){
            //2.1 有 更新问题
            question.setTxt(content);
            questionApi.updateById(question);
        }else {
            //2.2 没有 保存问题
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.add(question);
        }
    }
    //保存设置
    public void saveSetting(Settings settings) {
        //1.获取用户id
        Long userId = UserHolder.getUserId();
        //2.查询是否有设置
        Settings api = settingsApi.findByUserId(userId);
        //3、判断
        if(api == null) {
            //保存
            settings.setUserId(userId);
            settingsApi.save(settings);
        }else {
            //更新
            settings.setId(api.getId());
            settings.setUserId(userId);
            settingsApi.update(settings);
        }
    }

    public PageResult blacklist(int page, int size) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        PageResult pageResult = settingsApi.blackLimit(userId, page, size);
        return pageResult;
    }
    //取消黑名单
    public void deleteBlackById(Long bUid) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();

        blackListApi.deleteById(bUid,userId);
    }
}
